package fhfl.mobapp.belastungstest.mobapp_belastungstest;

import java.io.*;
import java.net.DatagramPacket;
import java.net.InetAddress;
import java.nio.ByteBuffer;

/**
 * Created by Iwo on 05.06.2014.
 */
public class ContentFactory
{
    private final int DATE_LENGTH = 8;
    private final byte CONTENT = 1;

    /**
     * Größe der Datenmenge in Byte
     */
    private int packetSize;
    private byte[] data = new byte[9];

    /**
     * Constructor which set the packetSize
     * @param packetSize Die Größe des Packets in Byte
     */
    public ContentFactory(int packetSize)
    {
        this.packetSize = packetSize;
        data = new byte[this.packetSize];
    }

    /**
     * Schreibt den aktuellen Timestamp + die Daten in den Stream
     * @param stream
     */
    public void writeTo(OutputStream stream) throws IOException
    {
        // Serialisierung des Datums von Long zu Byte[]
        byte[] time = ByteBuffer.allocate(DATE_LENGTH).putLong(System.currentTimeMillis()).array();

        for (int i = 0; i < DATE_LENGTH; i++)
            data[i] = time[i];

    }

    public synchronized byte[] createData()
    {
        byte[] time = ByteBuffer.allocate(DATE_LENGTH).putLong(System.currentTimeMillis()).array();

        for (int i = 0; i < DATE_LENGTH; i++)
            this.data[i] = time[i];

        for (int i = 0; i < data.length; i++)
            this.data[i + DATE_LENGTH] = CONTENT;
        return this.data;
    }

    public byte[] setPackage()
    {
        String time = new Long(System.currentTimeMillis()).toString();
        byte[] buffer = time.getBytes();

        return buffer;
    }

    /**
     * Gibt die Packetgröße in Byte zurück.
     * @return Die Packetgröße in Byte
     */
    public int getPacketSize()
    {
        return packetSize;
    }

    public void setNewPacketSize(int newPacketSize)
    {
        this.packetSize = newPacketSize;
    }
}
